// pages/shouye/listdetail/listdetail.js
const app = getApp();
var url = app.globalData.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    add:false,
    listData: [],
    listcart:[],
    prices:0,
    cupNumber: 0,
    items:0,
    totalPrice:0,
    detail:{},
    item:[]
  },
  goodsCarNumber:0,
  shoppingCarItem:[],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    console.log(id);
    var that = this;
    wx.request({
      url: url+'getDisherById',
      data: {
        id:id
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        res.data.object.disherIntroduction = app.convertHtmlToText(res.data.object.disherIntroduction);
        console.log(res.data.object)
        that.setData({
          detail: res.data.object,
        });
        that.query();    
      }
    })
  },
  //添加菜品到购物车
  addToCart:function(e){
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var price = e.currentTarget.dataset.price;
    var picPath = e.currentTarget.dataset.picpath
    const userid = app.globalData.userid;
   
    var that = this;
    wx.request({
      url: url +'insertGoodsInCartDing',
      data: {
        goodsId:id ,
        goodsName:name,
        goodsPrice:price,
        goodsNumber:1,
        goodsPic: picPath,
        cartType:1,
        userId: userid,
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log("点击触发")
        that.query();
        // that.setData({
        //   add:true
        // });
      }
    });
  },
  //查询购物车列表
  query:function(){
    const that = this;
    const userid = app.globalData.userid;
    wx.request({
      url: url + 'getShopCart',
      data: {
        userId: userid,
        cartType: 1,
        pageIndex:1,
        pageSize:100
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        that.goodsCarNumber = 0;
        if (res.data.object.cartList != undefined) {

          for (let i = 0; i < res.data.object.cartList.length; i++) {
            that.goodsCarNumber += res.data.object.cartList[i].goodsNumber
          }
          that.setData({
            goodsCarNumber: that.goodsCarNumber
          })
        } else {
          that.setData({
            goodsCarNumber: 0
          })
        }
        console.log(res.data.object.cartList)
        that.shoppingCarItem = res.data.object.cartList
        // this.data.listcart = res.data.object.cartList;
        // const cardata = res.data.object.cartList
        // if (cardata != undefined){ 
          
        //   var item = res.data.object.cartList.length;
        //   console.log(res.data.object.cartList)
        //   that.setData({
        //     goodsCarNumber:item,
        //     listcart: res.data.object
        //   });
        // }else{
        //   console.log('购物车是空的')
        //   that.setData({
        //     items: 0,
        //     listcart: ""
        //   });
        // }
        that.getTotalPrice();
      }
    });
  },
  //跳转购物车列表
  cart:function(){
    wx.navigateTo({
      url: '../cart/cart',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  //选好了
  goBalance:function(e){
    var listcart = this.shoppingCarItem;
    let arr = [];
    console.log(listcart)
    for (let i = 0; i < listcart.length; i++) {
      if (listcart[i].checked) {
        arr.push(listcart[i]);
      }
    }
    this.setData({
      item: arr,
    });
    console.log(this.data.item)
    var list = JSON.stringify(this.data.item);
    wx.navigateTo({
      url: '../makeorder/makeorder?list='+list,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 计算总价
   */
  getTotalPrice() {
    var that = this;
    var listcart = this.data.listcart;                 // 获取购物车列表
    var total = 0;
    for (var i = 0; i < listcart.length; i++) {        // 循环列表得到每个数据
      if (listcart[i]) {                     // 判断选中才会计算价格
        total += listcart[i].goodsNumber * listcart[i].goodsPrice;
        // 所有价格加起来
       }
    }
    that.setData({                               // 最后赋值到data中渲染到页面
      listcart: listcart,
      totalPrice: total
      // .toFixed(2)
    });
  }
})